$(document).ready(function(){
//显示|隐藏创建事件DIV
$("#eventCreateButton").click(createEventDiv);
function createEventDiv(){
	if($("#dynamicEventCreate").css("display")=="none"){
		//reset style
		$("#eventName").css({"border":"1px solid grey"});
		$("#eventStartDate").css({"border":"1px solid grey"});
		$("#eventEndDate").css({"border":"1px solid grey"});
		$("body").append("<div id='wholemark'></div>");
		var position = HHL.getPosition();
		var tl =(position.Tw-600)/2;
		var tt =(position.Th-300)/2;;
		$("#wholemark").css({"width":position.Tw,"height":position.Th});
		$("#dynamicEventCreate").css({"display":"block","left":tl,"top":tt});
	}else{
		$("#dynamicEventCreate").css({"display":"none"});
		$("#dynamicEventCreate").find("input[id='eventName']").val("");
		$("#dynamicEventCreate").find("input[id='eventStartDate']").val("");
		$("#dynamicEventCreate").find("input[id='eventEndDate']").val("");
		$("#dynamicEventCreate").find("textarea[id='eventDescription']").val("");
		$("#wholemark").remove();
	}
}
$("#eventCloseDiv").click(createEventDiv);
$("#createEventDo").click(doCreateEvent);
//创建事件
function doCreateEvent(){
	//reset style
	$("#eventName").css({"border":"1px solid grey"});
	$("#eventStartDate").css({"border":"1px solid grey"});
	$("#eventEndDate").css({"border":"1px solid grey"});
	var name = HHL.trim($("#eventName").val());
	if(!name){
		HHL.newAlertMes("请输入事件名称！","color:red;","1500");
		$("#eventName").css({"border":"1px solid red"});
		$("#eventName").focus();
		return false;
	}
	var startDateC = HHL.trim($("#eventStartDate").val());
	startDateC = HHL.dateVerify(startDateC);
	if(!startDateC){
		HHL.newAlertMes("日期格式不正确！","color:red;","1500");
		$("#eventStartDate").css({"border":"1px solid red"});
		$("#eventStartDate").focus();
		return false;
	}
	var endDateC = HHL.trim($("#eventEndDate").val());
	endDateC = HHL.dateVerify(endDateC);
	if(!endDateC){
		HHL.newAlertMes("日期格式不正确！","color:red;","1500");
		$("#eventEndDate").css({"border":"1px solid red"});
		$("#eventEndDate").focus();
		return false;
	}
	
	var description = $("#eventDescription").val();
	var startD= $("#eventStartDate").val();
	var endD= $("#eventEndDate").val();
	var url = "eventManageAjax.php";
	var data = {"postType":0,"name":name,"des":description,"startD":startD,"endD":endD};
	$.ajax({
		type:"POST",
		data:data,
		url:url,
		success:function(data){
			if(data){
				HHL.newAlertMes("事件创建成功","","1500");
				createEventDiv();
				//$("body").append(data);
				$(data).insertBefore($(".eventIterms").find("div:eq(0)"));
				$("#eventNone").remove();
			}else{
				HHL.newAlertMes("事件创建失败","","1500");
			}
		}
	})
}

//get event details information
window.EventDymhandle=0;
function getEventDetail(eventId,ob){
	if(!$("#targetEventInfo_"+eventId).html()){
		$("body").append("<div id='targetEventInfo_"+eventId+"' class='targetEvent'></div");
		$("#targetEventInfo_"+eventId).append("<div id=\"eventTargetCloseDiv\" onclick=\"eventTargetCloseDiv('"+eventId+"')\"><img src=\"style/window_close.png\" /></div>");
		EventDymhandle++;
		var position = HHL.getPosition();
		var left = (position.Tw-600)/2+EventDymhandle*10;
		var top = 50+EventDymhandle*10+$(document).scrollTop();
		var zindex = 5+EventDymhandle;
		$("#targetEventInfo_"+eventId).css({"left":left,"top":top,"z-index":zindex});
		var positionNew = HHL.getPosition();
		$("body").append("<div id='wholemark'></div>");
		$("#wholemark").css({"width":positionNew.Tw,"height":positionNew.Th});
		var eventControlDiv = "<div id='eventManageBar'><span onclick='eventUploadControl()'><img src='style/add.jpg' style='width:15px;height:15px;'>添加文件</span> <span onclick=\"deleteEvent('"+eventId+"')\" ><img src='style/no.png' />删除</span><div id='eventTargetFileUpload'><input type='file' name='eventFileUpload' id='eventFileUpload' /><input type='button' value='开始上传' onclick='uploadEventFiles()' id='startupload' /></div></div>";
		$("#targetEventInfo_"+eventId).append(eventControlDiv);
		var eventTargetInfoDiv= "<div id='targetEventInfoDetail_"+eventId+"' class='targetEventInfoDetail'></div>";
		$("#targetEventInfo_"+eventId).append(eventTargetInfoDiv);
		window.folder = eventId;
		window.parenFolder ="";
		window.eventId = eventId;
		loadUploadJs("eventFileUpload",folder,parenFolder,eventId);
		//ajax get target event information 
		getTargetEventInfo(eventId,"targetEventInfoDetail_"+eventId);
		
	}else{
		$("#targetEventInfo_"+eventId).remove();
		$("#wholemark").remove();
	}
}
window.getEventDetail = getEventDetail;

//delete event
function deleteEvent(eventId){
	window.eventId = eventId;
	var conhandle = confirm("确定删除？");
	if(!conhandle){
		return false;
	}
	var url = "eventManageAjax.php";
	var data = {'postType':4,'eventId':eventId};
	$.ajax({
		type:"POST",
		data:data,
		url:url,
		success:function(data){
			if(data){
				HHL.newAlertMes("删除成功！","z-index:20","2000");
				$("#targetEventInfo_"+eventId).remove();
				$("#eventId_"+eventId).remove();
				$("#eventnumber_"+eventId).remove();
				$("#wholemark").remove();
			}
		}
	})
}
window.deleteEvent=deleteEvent;
//ajax get target Event info DIV
function getTargetEventInfo(eventId,displayDiv){
	var url = "eventManageAjax.php";
	var data = {"postType":1,"eventId":eventId};	
	$.ajax({
		type:"POST",
		url:url,
		data:data,
		success:function(data){
			if($("#"+displayDiv).html()){
				$("#"+displayDiv).html("");
			}else{
				$("#"+displayDiv).html(data);
			}
		}
		})
}
window.getTargetEventInfo=getTargetEventInfo;

//close the dynamic event target div
function eventTargetCloseDiv(divId){
	$("#targetEventInfo_"+divId).remove();
	$("#wholemark").remove();
}
window.eventTargetCloseDiv=eventTargetCloseDiv;

function uploadEventFiles(){
	var scriptData = {'eventId': eventId,'parentFolder':parenFolder,'folder':folder};
	$('#eventFileUpload').uploadifySettings("scriptData",scriptData);
	$('#eventFileUpload').uploadifyUpload();
}
window.uploadEventFiles=uploadEventFiles;

function loadUploadJs(inputId,folder,parenFolder,eventId){
	window.eventId=eventId;
	window.folder = folder;
	window.parenFolder= parenFolder;
	$('#'+inputId).uploadify({
			'uploader'  : 'uploadify/uploadify.swf',
			'script'    : 'uploadFiles.php',
			'cancelImg' : 'uploadify/cancel.png',
			'buttonImg' : 'style/browse.jpg',
			'folder'    : folder,
			'scriptData' :{'eventId': eventId,'parentFolder':parenFolder},
			'auto'      : false,
			'fileExt'   : '*',
  			'fileDesc'  : 'Files',
			'multi'		:true,
			'removeCompleted':true,
			'onSelectOnce':function(event,data){
				$fileNumber = data.fileCount;
				if($fileNumber>0){
					if($("#startupload").css("display")=="none"){
						$("#startupload").css("display","inline");
					}
				}else{
					$("#startupload").css("display","none");
				}
			},
			'onCancel':function(event,ID,fileObj,data){
				$fileNumber = data.fileCount;
				if($fileNumber>0){
					if($("#startupload").css("display")=="none"){
						$("#startupload").css("display","inline");
					}
				}else{
					$("#startupload").css("display","none");
				}
			},
			'onAllComplete':function(event,data){
				getEventFilesD(eventId);
				HHL.newAlertMes("上传成功！","z-index:20","1500");
				$fileNumber = data.fileCount;
				if($fileNumber>0){
					if($("#startupload").css("display")=="none"){
						$("#startupload").css("display","inline");
					}
				}else{
					$("#startupload").css("display","none");
				}
			}
		});
}
window.loadUploadJs=loadUploadJs;

function getEventFilesD(eventId){
	var url= "eventManageAjax.php";
	var data ={"postType":3,"eventId":eventId};
	$.ajax({
		type:"POST",
		url:url,
		data:data,
		success:function(data){
			$("#targetEventInfoDetail_"+eventId).find("div[class='eInfoFiles']").html(data);
		}
	})
}
function eventUploadControl(){
	if($("#eventTargetFileUpload").css("display")=="none"){
		$("#eventTargetFileUpload").css({"display":"block"})
	}else{
		$("#eventTargetFileUpload").css({"display":"none"})
	}
}
window.eventUploadControl=eventUploadControl;

function downloadFile(parth){
	window.location.href="download/downloadFileAllKind.php?parth="+parth;
}
window.downloadFile = downloadFile;

//dispaly file control
function displayFileControl(ob){
	$(ob).find("a[class='fileManageA']").css({"display":"inline"});
}
window.displayFileControl=displayFileControl;
//hidden file control
function hiddenFileControl(ob){
	$(ob).find("a[class='fileManageA']").css({"display":"none"});
}
window.hiddenFileControl=hiddenFileControl;

function deleteEventFile(parth,ob){
	var confirmhandle = confirm("确定删除吗？");
	if(!confirmhandle){
		return false;
	}
	window.ob = ob;
	var url="eventManageAjax.php";
	var data = {"parth":parth,"postType":2};
	$.ajax({
	type:"POST",
	data:data,
	url :url,
	success:function(data){
		HHL.newAlertMes("删除成功","z-index:20","2000");
		$(ob).parent().remove();
	}
	})
	
}
window.deleteEventFile=deleteEventFile;
})